//
//  RenewDateChooserViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/21.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "RenewDateChooserViewController.h"

@interface RenewDateChooserViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) RenewDateChooserViewModel* viewModel;

// UI
//@property (weak, nonatomic) UILabel *addressLabel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UIImageView* tag;
@property (weak, nonatomic) IBOutlet UIButton* bgBtn;
@property (weak, nonatomic) IBOutlet UIButton* finishBtn;
@property (weak, nonatomic) IBOutlet UIButton* thirdMBtn;
@property (weak, nonatomic) IBOutlet UIButton* sixMBtn;
@property (weak, nonatomic) IBOutlet UIButton* oneYBtn;
@property (weak, nonatomic) IBOutlet UIButton* twoYBtn;

@end

@implementation RenewDateChooserViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.hidesBottomBarWhenPushed = YES;
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
}

- (void)initUI
{
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        UserManagerV2* userMgr = [UserManagerV2 sharedUserManagerV2];
    }];
    
    [didAppearSignal subscribeNext:^(id x)
     {
         @strongify(self)
         
         // ScrollView 内容 未能在此时才约束
         
         //Request
     }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:didAppearSignal] deliverOnMainThread] subscribeNext:^(id x) {
        @strongify(self)
        
        //重置 更新 frame
        [self _setupViews];
    }];
}

- (void)_setupViews
{
    @weakify(self);
    
    NSArray* btns = @[self.thirdMBtn, self.sixMBtn, self.oneYBtn, self.twoYBtn];
    
    for (UIButton* curBtn in btns)
    {
        curBtn.layer.borderColor = UIColorHex(#c1e2d5).CGColor;
        curBtn.layer.borderWidth = 1.0f;
        [curBtn addTarget:self action:@selector(_onDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.finishBtn addTarget:self action:@selector(_onBtnFinish) forControlEvents:UIControlEventTouchUpInside];
    
    [self.bgBtn addTarget:self action:@selector(_onBtnBg) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- sub class can override it
- (UIEdgeInsets)contentInset
{
    return UIEdgeInsetsMake(64, 0, 0, 0);
}

#pragma mark - 监听
- (void)_onDateBtn:(id)sender
{
    NSArray* btns = @[self.thirdMBtn, self.sixMBtn, self.oneYBtn, self.twoYBtn];
    
    NSInteger count = btns.count;
    for (NSInteger index=0; index<count; index++)
    {
        UIButton* curBtn = btns[index];
        
        BOOL isSelect = NO;
        if (curBtn == sender)
        {
            isSelect = YES;
            
            self.viewModel.selectIndex = index;
            
            CGSize tagSize = self.tag.frame.size;
            CGFloat tagX = CGRectGetMaxX(curBtn.frame) - tagSize.width;
            CGFloat tagY = CGRectGetMaxY(curBtn.frame) - tagSize.height;
            self.tag.frame = (CGRect) {tagX, tagY, tagSize};
        }
        
        curBtn.backgroundColor = isSelect ? UIColorHex(#dafef0) : [UIColor clearColor];
    }
}

- (void)_onBtnFinish
{
    [self.viewModel.finishCmd execute:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)_onBtnBg
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - 工具

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
